import 'package:flutter/material.dart';
import 'package:flutter_09_game/pages/main/keep_alive_widget.dart';
import 'package:flutter_09_game/widgets/tab/common_tab_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

// import 'package:flutter_base/widgets/tab/common_tab_bar.dart';
// import 'package:flutter_base/widgets/tab/custom_tab_bar_view.dart';

class CommonTabBarPage extends StatefulWidget {
  const CommonTabBarPage(
      {super.key,
      required this.tabController,
      required this.tabData,
      this.tabBarPadding,
      this.tabBarLeftView,
      this.tabBarRightView,
      this.onTap,
      this.showBadge = const [],
      this.tabBarHeight,
      this.labelPadding,
      // this.reverse = false,
      this.tabIsScrollable = true,
      this.fitTabAlignment,
      this.fitTabBackgroundColor,
      this.fitTabHeight,
      this.fitTabWidth,
      this.fitBorderRadius,
      this.tabBarContentPadding,
      this.indicator,
      this.indicatorPadding = EdgeInsets.zero,
      this.indicatorSize,
      this.labelStyle,
      this.unselectedLabelStyle,
      this.isCover = false,
      this.unselectedLabelColor,
      this.labelColor,
      this.topWidget,
      this.topWidgetHeight = 0,
      this.topPadding = 0,
      this.topSafe = false,
      this.keepView = true,
      this.backgroundColor,
      this.showLine = false});

  final TabController tabController;
  final List<CommonTabModel> tabData;
  final EdgeInsetsGeometry? tabBarPadding; //整个内容的padding
  final Widget? tabBarLeftView;
  final Widget? tabBarRightView;
  final void Function(int)? onTap;
  final List<bool?> showBadge;
  final double? tabBarHeight;
  final Color? unselectedLabelColor;
  final Color? labelColor;
  final bool showLine;
  final Color? backgroundColor;

  ///tar和view true层叠  false 上下
  final bool isCover;
  final bool topSafe;
  final double topPadding;
  final Widget? topWidget;
  final double topWidgetHeight;

  // final bool reverse;
  final EdgeInsetsGeometry? labelPadding;
  final bool tabIsScrollable;
  final AlignmentGeometry? fitTabAlignment; //tab不完全填充的位置
  final Color? fitTabBackgroundColor; //tab不完全填充的背景色
  final double? fitTabHeight; //tab不完全填充的高度
  final double? fitTabWidth; //tab不完全填充的宽度
  final double? fitBorderRadius; //tab不完全填充的圆角
  final EdgeInsetsGeometry?
      tabBarContentPadding; //仅仅CommonTabBar,不包含左右组件的padding
  final TextStyle? unselectedLabelStyle;
  final TextStyle? labelStyle;
  final Decoration? indicator;
  final EdgeInsetsGeometry indicatorPadding;
  final TabBarIndicatorSize? indicatorSize;
  final bool keepView;

  @override
  State<CommonTabBarPage> createState() => _CommonTabBarPageState();
}

class _CommonTabBarPageState extends State<CommonTabBarPage> {
  // int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    // _currentIndex = widget.tabController.index;
  }

  @override
  Widget build(BuildContext context) {
    return widget.isCover
        ? Stack(
            children: [
              Positioned.fill(child: _buildMain()),
              Positioned(
                top: 0,
                left: 0,
                right: 0,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    widget.topWidget ?? const SizedBox.shrink(),
                    widget.topSafe && widget.topWidget == null
                        ? SafeArea(
                            child: _buildTabBar(),
                          )
                        : Padding(
                            padding: EdgeInsets.only(top: widget.topPadding),
                            child: _buildTabBar(),
                          ),
                  ],
                ),
              )
            ],
          )
        : Column(
            children: [
              _buildTabBar(),
              Visibility(
                visible: widget.showLine,
                child: const Divider(
                  height: 0.5,
                  color: Colors.black12,
                ),
              ),
              Expanded(child: _buildMain()),
            ],
          );
  }

  Widget _buildMain() {
    return TabBarView(
      controller: widget.tabController,
      children: widget.tabData.map((e) {
        if (widget.isCover && widget.topSafe && e.isCover == false) {
          return SafeArea(
              child: Padding(
            padding: EdgeInsets.only(
              top: (widget.tabBarHeight ?? 44.w) + widget.topWidgetHeight,
            ),
            child: _buildKeepView(
              e.view,
            ),
          ));
        } else {
          return _buildKeepView(
            e.view,
          );
        }
      }).toList(),
    );
  }

  Widget _buildKeepView(Widget child) {
    // if (widget.keepView) {
    //   return child;
    // } else {
      return KeepAliveWrapper(keepAlive: widget.keepView,child: child);
    // }
  }

  Widget _buildTabBar() {
    Widget tabBar = CommonTabBar(
      labelPadding: widget.labelPadding,
      tabController: widget.tabController,
      isScrollable: widget.tabIsScrollable,
      // unselectedLabelColor: widget.un,
      tabContentPadding: widget.tabBarContentPadding,
      tabs: _buildTabs(),
      labelColor: widget.labelColor,
      unselectedLabelColor: widget.unselectedLabelColor,
      unselectedLabelStyle: widget.unselectedLabelStyle,
      labelStyle: widget.labelStyle,
      indicator: widget.indicator,
      indicatorPadding: widget.indicatorPadding,
      indicatorSize: widget.indicatorSize,

      onTap: (index) {
        widget.onTap?.call(index);
        // if (index != _currentIndex) {
        //   _currentIndex = index;
        //   if (mounted) {
        //     setState(() {});
        //   }
        // }
      },
    );
    if ((widget.fitTabWidth ?? 0) > 0) {
      tabBar = Align(
        alignment: widget.fitTabAlignment ?? Alignment.center,
        child: Container(
          width: widget.fitTabWidth,
          height: widget.fitTabHeight,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: widget.fitTabBackgroundColor,
            borderRadius: BorderRadius.circular(widget.fitBorderRadius ?? 0),
          ),
          child: tabBar,
        ),
      );
    }
    return Container(
      padding: widget.tabBarPadding ?? EdgeInsets.symmetric(vertical: 8.w),
      height: widget.tabBarHeight ?? 50.w,
      color: widget.backgroundColor,
      child: Row(
        children: [
          if (widget.tabBarLeftView != null) widget.tabBarLeftView!,
          Expanded(
            child: tabBar,
          ),
          if (widget.tabBarRightView != null) widget.tabBarRightView!,
        ],
      ),
    );
  }

  List<Widget> _buildTabs() {
    final List<Widget> list = [];
    for (var i = 0; i < widget.tabData.length; i++) {
      CommonTabModel model = widget.tabData[i];
      // bool isSelected = _currentIndex == i;
      // final tab = Badge(
      //   isLabelVisible: widget.showBadge.elementAtOrNull(i) ?? false,
      //   child: _buildTab(model, isSelected),
      // );
      list.add(_buildTab(model));
    }
    return list;
  }

  Widget _buildTab(
    CommonTabModel model,
  ) {
    return Tab(
      child: Text(
        model.title != null
            ? (model.isMulti == true ? model.title!.tr : model.title!)
            : widget.tabData.indexOf(model).toString(),
      ),
    );
    // }
  }
}

class CommonTabModel {
  CommonTabModel({
    required this.view,
    this.title,
    this.tab,
    this.selectTab,
    this.style,
    this.id,
    this.selectStyle,
    this.isMulti = true,
    this.isCover = false,
  }) : assert(title != null || tab != null, "title 或 tab 必须传递一个");

  Widget view;
  String? title;
  int? id;
  TextStyle? style;
  TextStyle? selectStyle;
  Widget? tab;
  Widget? selectTab;
  bool? isMulti;
  bool? isCover;
}
